﻿using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using TianYuan.Core.Dependency;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TianYuan.Data.Ef.Dependency
{
    internal class EfConventionalRegistrar : IConventionalDependencyRegistrar
    {
        public void RegisterAssembly(IConventionalRegistrationContext context)
        {
            //context.IocManager.IocContainer.Register(
            //Component.For<IDbContext>().ImplementedBy<DapperContext>().LifestylePerWebRequest(),
            //Component.For<IDapperUnitOfWork>().ImplementedBy<UnitOfWork>().LifestyleTransient(),
            //Component.For(typeof(IDapperRepository<>)).ImplementedBy(typeof(Repository<>)).LifestyleTransient()
            //);
            //Transient
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ITransientDependency>()
                    .WithService.Self()
                    //.WithService.DefaultInterfaces()  //实现类命名必须规范
                    .WithService.AllInterfaces()    //实现类命名不必须规范
                    .LifestyleTransient()
                );
            //PerWebRequest
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<IPerWebRequestDependency>()
                    .WithService.Self()
                     //.WithService.DefaultInterfaces()
                     .WithService.AllInterfaces()
                    .LifestylePerWebRequest()
                );
            //Singleton
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ISingletonDependency>()
                    .WithService.Self()
                     //.WithService.DefaultInterfaces()
                     .WithService.AllInterfaces()
                    .LifestyleSingleton()
                );

            //Windsor Interceptors
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<IInterceptor>()
                    .WithService.Self()
                    .LifestyleTransient()
                );
        }
    }
}
